const app = getApp();
const {
  getBannerList,
  getLiveList
} = require('../../api/index-api')
const {
  getUserSig
} = require('../../api/common.api')

var liveroom = require('../../components/mlvb-live-room/mlvbliveroomcore.js');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    bannerList: [],
    liveList: [],
    livePage: 1,
    livePageSize: 10,
    liveTotalPage: 0,
    tapTime: '',
    roomServiceIsLogin: false,
    userName: '',
    userID: '',
    userAvatar: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 轮播图中转
   */
  onBannerToGo(e) {
    let path = e.currentTarget.dataset.path
    wx.navigateTo({
      url: `../web-view/index?url=${path}`,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let _this = this;
    // 初次获取轮播图
    getBannerList().then(res => {
      _this.setData({
        bannerList: res.list,
      })
    })
    

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    
    let _this = this;
    let params = {
      page: _this.data.livePage,
      page_size: _this.data.livePageSize
    }
    getLiveList(params).then(res => {
      _this.setData({
        liveList: res.list,
        livePage: res.page,
        livePageSize: res.page_size,
        liveTotalPage: res.total_page,
      })
    })

    // 登录roomService
    getUserSig().then(res => {
      let _this = this,
        loginInfo = {
          sdkAppID: res.sdk_app_id,
          userID: res.userid,
          userSig: res.user_sig,
          userName: res.user_name,
          userAvatar: res.user_avatar
        }
      liveroom.login({
        data: loginInfo,
        success: function (ret) {
          _this.setData({
            roomServiceIsLogin: true,
            userID: res.userid,
            userName: res.user_name,
            userAvatar: res.user_avatar
          })
          app.globalData.roomServiceIsLogin = true;
        },
        fail: function (ret) {
          //登录失败
          _this.data.roomServiceIsLogin = false;
          
          wx.showModal({
            title: '提示',
            content: ret.errMsg,
            showCancel: false,
            complete: function () {
              wx.navigateBack({});
            }
          });
        }
      });
    })
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // liveroom.logout();
    this.setData({
      livePage: 1,
    })
  },
  onShareTimeline: function () { 
    return {
      title: `来测知小程序看直播吧`,
      path: '/pages/index/index',
      imageUrl: 'https://cebeiqiniu.nmhywl.cn/image/5a29073faa378d01547e9ae21079f316.png'
    }
  } ,
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.setData({
      livePage: 1,
    })
    // liveroom.logout();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let _this = this;
    if (_this.data.livePage < _this.data.liveTotalPage) {
      let params = {
        page: _this.data.livePage + 1,
        page_size: _this.data.livePageSize
      }
      getLiveList(params).then(res => {
        let newLiveList = _this.data.liveList.concat(res.list)
        _this.setData({
          liveList: newLiveList,
          livePage: res.page,
          livePageSize: res.page_size,
          liveTotalPage: res.total_page,
        })
        // console.log(res)
      })
    } else {
      wx.showToast({
        title: '我是有底线的~',
        duration: 1500,
        icon: 'none'
      })
    }
  },
  /**
   * 去直播间 new SDK
   */
  onGoLiveRoom: function (e) {
    let _this = this;
    const {
      roomid,
      roomname,
      diamonds,
      username,
      userid,
      type,
      cover,
      avatar,
    } = e.currentTarget.dataset;
    console.log(avatar)
    // live_id = e.currentTarget.dataset.id
    // 防止两次点击操作间隔太快
    var nowTime = new Date();
    if (nowTime - this.data.tapTime < 1000) {
      return;
    }
    let pureAudio = type == 1 ?  true: false;
    if (_this.data.roomServiceIsLogin) {
      let url = `../live/live-room/index?roomID=${roomid}&roomName=${roomname}&userName=${username}&userID=${userid}&avatar=${avatar}&diamonds=${diamonds}&pureAudio=${pureAudio}&cover=${cover}`;
      wx.redirectTo({
        url: url,
      })
      // wx.navigateTo({
      //   url: url
      // });
      wx.showToast({
        title: '进入房间',
        icon: 'success',
        duration: 1000
      })
    }else{
      // 重新登录roomService
    }
    this.setData({
      'tapTime': nowTime
    });
  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: `来测知小程序看直播吧`,
      path: '/pages/index/index',
      imageUrl: 'https://cebeiqiniu.nmhywl.cn/image/5a29073faa378d01547e9ae21079f316.png'
    }
  }
})